<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* 
*/
class UserMember extends MY_Controller
{
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('User/UserModel','user');
	}


	//会员列表
	public function index()
	{
		$query = $this->db->query("SELECT info FROM nmp_config where name = 'user_examine'");
		$data = $query->row_array();

		$this->load->view('user_list', $data);
	}


	//获取列表数据
	public function get_list()
	{
		$page = $this->input->get('page');
		$page = $page ? $page : 1;

		$term['keyword'] = $this->input->get('keyword');

		$res = $this->user->get_user_data($term,$page);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//消费情况
	public function member_info()
	{
		$user_id = intval($this->uri->segment(3));
		//获取会员基本信息
		$data = $this->user->user_data_row($user_id); 
		//获取会员消费情况
		$consu = $this->user->get_user_consu($user_id);

		$data['total_num'] = $consu['total_num'];
		$data['month_total'] = $consu['month_total'];
		$data['course_num'] = $consu['course_num'];
		$data['balance'] = $consu['balance'];
		$data['card_num'] = $consu['card_num'];
		$data['user_id'] = $user_id;

		$this->load->view('member_info', $data);
	}


	//课程情况
	public function get_course(){
		
		$user_id = intval($this->uri->segment(3));
		
		
		$res = $this->user->get_course_card($user_id);
		
		if ($res) 
		{
			$data['list'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}



	//获取消费记录
	public function get_consu_log()
	{
		$user_id = intval($this->uri->segment(3));
		$res = $this->user->consu_log($user_id);

		if ($res) 
		{
			$data['list'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();
	}


	//获取折线图数据
	public function get_appoint()
	{
		$user_id = $this->input->get('user_id');
		$date_num = $this->input->get('date_num');

		$res = $this->user->appoint_data($user_id,$date_num);

		if ($res) 
		{
			$data = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}


	//预约记录
	public function get_bespeak()
	{
		$user_id = intval($this->uri->segment(3));
		$res = $this->user->bespeak_data($user_id);

		if ($res) 
		{
			$data['list'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}


	//训练计划
	public function get_plan()
	{
		$user_id = intval($this->uri->segment(3));
		$res = $this->user->plan_data($user_id);

		if ($res) 
		{
			$data['list'] = $res;
			$data['status'] = 200;
			$data['message'] = '获取成功！';
		}
		else
		{
			$data['list'] = '';
			$data['status'] = 400;
			$data['message'] = '数据获取失败！';
		}

		echo json_encode($data);exit();

	}

    /**
     * 查看训练计划详情
     */
	public function plan_info()
    {
        $plan_id = intval($this->uri->segment(3));
        $res['data'] = $this->user->plan_info($plan_id);


        $this->load->view('plan_info', $res);
    }


	//是否通过
	public function set_disabled()
	{
		$uid = $this->input->get('uid',TRUE);
		$set_val = $this->input->get('disabled', TRUE);

		$res = $this->user->set_user_disabled($uid,$set_val);

		if ($res) 
		{
			$data['status'] = 200;
			$data['message'] = '操作成功！';
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '操作失败！';
		}

		echo json_encode($data);exit();
	}


	//设置是否需要审核
	public function set_examine(){
		
		$examine = $this->input->get('examine',TRUE);

		$res = $this->user->examine_config($examine);

		if($res){
			
			$data['status'] = 200;
			$data['message'] = '操作成功！';
			
		}
		else
		{
			$data['status'] = 400;
			$data['message'] = '操作失败！';
		}

		echo json_encode($data);exit();
	}
	
	//添加会员
	function add(){
		
		$farr = array('mobile','nickname','sex','city','province','birthday','info');
		$pdata = $this->input->post();
		
		$arr = array();
		foreach($pdata as $key=>$val){
			if(in_array($key,$farr)){
				$arr[$key] = $val;
			}
		}
		$filter = array(
			'mobile'=>array(
				'required'=>'手机号码不能为空',
				"regex_match[/^1[34578]\d{9}$/]"=>'请输入正确的手机号码'
			)
		);
		$result = $this->CheckData($filter,$arr);
		if($result!==false){
			$this->err_out($result);
		}
		
		
		$is_exist = $this->user->_where_exist(array('mobile'=>$arr['mobile']));
		if($is_exist){
			$this->err_out('该手机号码已经存在');
		}
		
		$arr['addtime'] = time();
		
		$this->user->add($arr);
		
		$this->data_out(1);
		
	}

	function add_card(){
		
		$user_id = $this->input->post('uid',null,0);
		$card_id = $this->input->post('card_id',null,0);
		$num = $this->input->post('num',null,0);
		
		$this->load->model('Courses/CourseCardModel','card');
		
		$card = $this->card->_id_get_info($card_id);
		if(!$card){
			$this->err_out('课程卡不存在');
		}
		if($num>$card['course_num']){
			$this->err_out('剩余次数不能大于课程卡的次数');
		}
		
		$time = time();
		
		
		$this->load->dmodel('user_card_log','user_card_log');
		$logarr = array(
			'user_id'=>$user_id,
			'card_id'=>$card_id,
			'price'=>$card['price'],
			'num'=>$card['course_num'],
			'info'=>$card_id,
			'addtime'=>$time,
		);
		$this->user_card_log->add($logarr);
		
		$this->load->dmodel('user_card','user_card');
		
		$arr = array(
			'user_id'=>$user_id,
			'cid'=>$card['course_id'],
			'card_id'=>$card_id,
			'total_num'=>$card['course_num'],
			'num'=>$num,
			'price'=>$card['price'],
			'addtime'=>$time
			
		);
		$this->user_card->add($arr);
		
		
		
		
		
		$this->data_out(1);
	}
}




?>